package cn.szzsi.platform.service;

import cn.szzsi.model.AgentBalanceDetail;
import cn.szzsi.model.Member;
import cn.szzsi.model.PlateformConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import org.apache.commons.lang3.ArrayUtils;

import java.math.BigDecimal;

public class AgentService {
    public static final AgentService service=new AgentService();

    /**
     * 当有代理商加入时,给推荐人返佣
     * 必须满足条件,代理商已经审核通过,代理商等级必须大于等于当前返佣等级
     */
    public void recommendMoneyToAgent(Member member){

        PlateformConfig plateformConfig=PlateformConfig.dao.findById(1);
        Integer agentLevel=member.getAgentLevel();//当前代理商等级
        BigDecimal agentMoney=new BigDecimal(0);//代理商缴纳金额
        if(1==agentLevel){
            agentMoney=plateformConfig.getOneAgentMoney();
        }else if(2==agentLevel){
            agentMoney=plateformConfig.getTwoAgentMoney();
        }else if(3==agentLevel){
            agentMoney=plateformConfig.getThreeAgentMoney();
        }
        BigDecimal firstAgentRecommendMoney=plateformConfig.getFirstAgentRecommendPercent().divide(new BigDecimal(100)).multiply(agentMoney);//直属(一级)代理商推荐收益
        BigDecimal secondAgentRecommendMoney=plateformConfig.getSecondAgentRecommendPercent().divide(new BigDecimal(100)).multiply(agentMoney);//直属(二级)代理商推荐收益
        BigDecimal thirdAgentRecommendMoney=plateformConfig.getThirdAgentRecommendPercent().divide(new BigDecimal(100)).multiply(agentMoney);////直属(二级)代理商推荐收益

        //直属代理商返佣开始
        String firstAgentPid=member.getAgentPid();//直属上级
        if(StrKit.notBlank(firstAgentPid)&& !"0".equals(firstAgentPid)){
            Member firstMember=Member.dao.findById(firstAgentPid);
            if(firstMember!=null){
                if(2==firstMember.getAgentStates()){//满足审核通过
                    String firstMemberId=firstMember.getId();
                    AgentBalanceService.service.updateBalance(firstMemberId,firstAgentRecommendMoney);//更新余额
                    AgentBalanceDetailService.service.saveInfo(firstMemberId,firstAgentRecommendMoney,1,member.getId(),"","",1,"直属代理商推荐返佣");
                }
                //直属代理商返佣结束

                //直属二级代理商返佣开始
                String secondAgentPid=firstMember.getAgentPid();//直属二级
                if(StrKit.notBlank(secondAgentPid) && !"0".equals(secondAgentPid)){
                    Member secondMember=Member.dao.findById(secondAgentPid);
                    if (secondMember!=null){
                        if(2==secondMember.getAgentStates()&& secondMember.getAgentLevel()>=2){//审核通过,等级大于返佣等级
                            String secondMemberId=secondMember.getId();
                            AgentBalanceService.service.updateBalance(secondMemberId,secondAgentRecommendMoney);//更新余额
                            AgentBalanceDetailService.service.saveInfo(secondMemberId,secondAgentRecommendMoney,1,member.getId(),"","",2,"直属二级代理商推荐返佣");
                        }
                        //直属二级代理商返佣结束

                        //直属三级代理商返佣开始
                        String thirdAgentPid=secondMember.getAgentPid();
                        if(StrKit.notBlank(thirdAgentPid) && !"0".equals(thirdAgentPid)){
                            Member thirdMember=Member.dao.findById(thirdAgentPid);
                            if(thirdMember!=null){
                                if(2==thirdMember.getAgentStates() && thirdMember.getAgentLevel()>=3){
                                    String thirdMemberId=thirdMember.getId();
                                    AgentBalanceService.service.updateBalance(thirdMemberId,thirdAgentRecommendMoney);//更新余额
                                    AgentBalanceDetailService.service.saveInfo(thirdMemberId,thirdAgentRecommendMoney,1,member.getId(),"","",3,"直属三级代理商推荐返佣");
                                }
                                //直属三级代理商返佣结束
                            }
                        }
                    }
                }


            }

        }

    }

    public static void main(String[] args) {
        float n=10;
        BigDecimal percent=(new BigDecimal(n)).divide(new BigDecimal(100));
    }
}
